<?php
namespace app\wechat\controller;
use app\api\service\Brand;
use app\api\service\MemberCar;

class WMemberCar extends WCommon
{
    // 车辆列表
    public function index() {
        $param = [
            'member_id' => session('member_id')
        ];
        $my_car = MemberCar::get_my_car($param);
        if ($my_car['code'] != 200) {
            $data = [];
        } else {
            $data =  $my_car['data'];
        }
        $this->assign('data', $data);
        return view();
    }

    // 品牌列表
    public function brand_list() {
        $brands = Brand::brand_list();
        //dump($brands);
        if ($brands['code'] == 200)
            $this->assign('brands', $brands['data']);
        else
            $this->assign('brands', []);
        return view();
    }

    // 品牌型号列表
    public function brand_model_list() {
        $brand_id = input('param.brand_id');
        $param = ['brand_id' => $brand_id];
        $models = Brand::brand_model_list($param);
        $this->assign('models', $models['data']);
        return view();
    }

    // 编辑我的爱车
    public function save_my_car() {
        $brand_id = input('param.brand_id');
        $model_id = input('param.model_id');
        $id = input('param.id', 0);

        if (request()->isAjax()) {
            $param = [
                'id' => input('param.id'),
                'member_id' => session('member_id'),
                'brand_id' => input('param.brand_id'),
                'model_id' => input('param.model_id'),
                'name' => input('param.name'),
                'image' => input('param.image', ''),
                'mileage' => input('param.mileage'),
                'displacement' => input('param.displacement'),
                'production_time' =>strtotime(input('param.production_time')) ,
                'start_off_time' => strtotime(input('param.start_off_time')),
                'plate' => input('param.plate'),
                'location' => input('param.location')
            ];
            $data = MemberCar::save_my_car($param);
            return json($data);
        } else {
            // 获取品牌信息
            $param = [
                'brand_id' => $brand_id
            ];
            $brand = Brand::get_brand_info($param);
            $this->assign('brand', $brand['data']);
            // 获取型号信息
            $param = [
                'model_id' => $model_id
            ];
            $model = Brand::get_brand_model_info($param);
            $this->assign('model', $model['data']);

            $my_car = [];
            if ($id > 0) {
                $param = [
                    'car_id' => $id,
                    'member_id' => session('member_id')
                ];
                // 获取车辆信息
                $data = MemberCar::get_my_car_info($param);
                if ($data['code'] == 200) {
                    $my_car = $data['data'];
                }
            }
            $this->assign('my_car', $my_car);

            return view();
        }
    }

    // 设置默认车辆
    public function save_default_car() {
        $param = [
            'id' => input('param.id'),
            'member_id' => session('member_id'),
            'is_default' => input('param.is_default')
        ];
        // 设置车辆默认状态
        $data = MemberCar::save_default_car($param);

        return json($data);
    }

    // 我的爱车详情
    public function my_car_info() {
        $id = input('param.id');
        $param = [
            'car_id' => $id,
            'member_id' => session('member_id')
        ];
        // 获取车辆信息
        $data = MemberCar::get_my_car_info($param);
        if ($data['code'] == 200) {
            $data = $data['data'];
        } else {
            $data = [];
        }
        $this->assign('data', $data);
        return view();
    }
}
